//
//  ChooseLetterViewController.m
//  HotWord
//
//  Created by Rainbow on 8/25/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ChooseLetterViewController.h"
#import "LearningViewController.h"
#import "PracticeViewController.h"
#import "CacheData.h"
#import "HotWordAppDelegate.h"
#import "MaskView.h"

@implementation ChooseLetterViewController
@synthesize backView;

- (void)dealloc
{
    [super dealloc];
}
//翻转横幅选择进度
- (void)FilpToChoose:(id)sender
{
    // NSLog(@"come into filptochoose !");
    UIButton *actionButton = (UIButton *)sender;
    int destTag = actionButton.tag;
    
    [self.view viewWithTag:destTag-50 ];
    [self.view viewWithTag:destTag+50 ];
    
    //new here
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationRepeatAutoreverses:NO];
    [[self.view viewWithTag:destTag-250]setAlpha:0.0f];
    [[self.view viewWithTag:destTag-50 ]setAlpha:0.0f];
    [[self.view viewWithTag:destTag+50 ] setAlpha:0.0f];
    [[self.view viewWithTag:destTag+150] setAlpha:1.0f];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
    
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDelay:0.3f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationRepeatAutoreverses:NO];
    [self.view viewWithTag:destTag-100 ].alpha=1.0f;
    [self.view viewWithTag:destTag-200 ].alpha=1.0f;
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
    
    
    CATransition *animation = [CATransition animation];
    animation.delegate = [self.view viewWithTag:destTag+100 ] ;
    animation.duration = 0.6f;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.fillMode = kCAFillModeForwards;
    animation.type = @"oglFlip";
    
    
    if (flag!=0) {
        [(UIImageView *)[self.view viewWithTag:flag-400] setImage:[UIImage imageNamed:@"Learn_common.png"]];
        [[self.view viewWithTag:flag-400].layer addAnimation:animation forKey:@"animation"];
        [self.view viewWithTag:flag].hidden=NO;
        
        //new here
        
        
        
        
        [[self.view viewWithTag:flag-300].layer addAnimation:animation forKey:@"animation"];
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelay:0.4f];
        [UIView setAnimationRepeatAutoreverses:NO];
        [[self.view viewWithTag:flag-250]setAlpha:1.0f];
        
        
        
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
        
        
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        
        [UIView setAnimationRepeatAutoreverses:NO];
        
        
        [self.view viewWithTag:flag-100].alpha=0.0f;
        [self.view viewWithTag:flag-200].alpha=0.0f;
        
        [[self.view viewWithTag:flag-50 ]setAlpha:1.0f];
        [[self.view viewWithTag:flag+50 ] setAlpha:1.0f];
        [[self.view viewWithTag:flag+150] setAlpha:0.0f];
        
        
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
        
        
        
    }
    [(UIImageView *)[self.view viewWithTag:destTag-400] setImage:[UIImage imageNamed:@"Learn_show.png"]];
    [[self.view viewWithTag:destTag] setHidden:YES];
    
    //new here
    [[self.view viewWithTag:destTag-300].layer addAnimation:animation forKey:@"animation"];
    [[self.view viewWithTag:destTag-400 ].layer addAnimation:animation forKey:@"animation"];
    flag=destTag;
    
    [self showBtn];
    
    
    
}


-(void)hiddenTwoBtn
{
    
    [self.view viewWithTag:flag-100].hidden=YES;
    [self.view viewWithTag:flag-200].hidden=YES;
    [self.view viewWithTag:flag-100].alpha=1.0f;
    [self.view viewWithTag:flag-200].alpha=1.0f;
    
}



-(void)showBtn
{
    [self.view viewWithTag:flag-100].hidden=NO;
    [self.view viewWithTag:flag-200].hidden=NO;
    [self.view viewWithTag:flag-100].alpha=1.0f;
    [self.view viewWithTag:flag-200].alpha=1.0f;
    
}



-(void)viewWillAppear:(BOOL)animated
{
    MaskView *maskView = [[[MaskView alloc] init] autorelease];
    maskView = [[[NSBundle mainBundle] loadNibNamed:@"MaskView" owner:self options:nil] objectAtIndex:0];
    [self.view addSubview:maskView];
    maskView.frame = CGRectMake(0, 330, 320, 150);
    [self.view bringSubviewToFront:maskView];
    maskView.alpha = 1;
    
    backView.frame=CGRectMake(0,0,320,480);
    
    HotWordAppDelegate *appDelegate= (HotWordAppDelegate *)[UIApplication sharedApplication].delegate;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.2f];
    [appDelegate.MainToolBar viewWithTag:66].alpha=0.0f;
    [appDelegate.MainToolBar viewWithTag:77].alpha=0.0f;
    [appDelegate.MainToolBar viewWithTag:88].alpha=0.0f;
    [UIView setAnimationDidStopSelector:@selector(updateToolBtn)];
    [UIView commitAnimations];
    
    
    
    
    for (int i = 26; i >= 0; i--)
    {
        
        Letter *letter = [[CacheData sharedData].savedLetters objectForKey:[[CacheData sharedData].letters objectAtIndex:i]];
        int wordsCount= [letter.words count];
        NSInteger currentIndex = 0;
        if ([CacheData sharedData].viewType == HotWordViewTypeLearn)
        {
            currentIndex=[letter.learnIndex intValue];
        }
        else
        {
            currentIndex=[letter.practiceIndex intValue];
        }
        
        currentIndex=currentIndex+1;
        if (wordsCount == 0) {
            currentIndex = 0;
        }
        
        
        [(UILabel *)[self.view viewWithTag:450+i] setFont:[UIFont fontWithName:@"Thonburi" size:20]];
        
        ((UILabel *)[self.view viewWithTag:450+i]).textColor= [UIColor colorWithRed:104.0/255.0 green:198.0/255.0 blue:226.0/255.0 alpha:1];
        [(UILabel *)[self.view viewWithTag:550+i] setFont:[UIFont fontWithName:@"Thonburi" size:20]];
        
        
        ((UILabel *)[self.view viewWithTag:450+i]).textAlignment = UITextAlignmentRight;
        ((UILabel *)[self.view viewWithTag:550+i]).textAlignment = UITextAlignmentLeft;
        [((UILabel *)[self.view viewWithTag:650+i]) setFont:[UIFont fontWithName:@"Thonburi" size:20]];
        ((UILabel *)[self.view viewWithTag:650+i]).textColor=[UIColor whiteColor];
        ((UILabel *)[self.view viewWithTag:650+i]).alpha=0.0f; 
        
        
        
        ((UILabel *)[self.view viewWithTag:650+i]).textAlignment = UITextAlignmentCenter;
        
        ((UILabel *)[self.view viewWithTag:450+i]).text=[[NSString alloc]initWithFormat:@"%d",currentIndex];
        ((UILabel *)[self.view viewWithTag:550+i]).text=[[NSString alloc]initWithFormat:@"| %d",wordsCount];
        
        ((UILabel *)[self.view viewWithTag:650+i]).text=[[NSString alloc]initWithFormat:@"%@ %@",((UILabel *)[self.view viewWithTag:450+i]).text,((UILabel *)[self.view viewWithTag:550+i]).text];

        
    }   
    
}

-(void)updateToolBtn
{   
    HotWordAppDelegate *appDelegate= (HotWordAppDelegate *)[UIApplication sharedApplication].delegate;
    [[appDelegate.MainToolBar viewWithTag:77]removeFromSuperview];
    [[appDelegate.MainToolBar viewWithTag:66]removeFromSuperview];
    [[appDelegate.MainToolBar viewWithTag:88]removeFromSuperview];
    
    UIButton *backToMenuBtn=[[[UIButton alloc]init] autorelease];
    backToMenuBtn.frame=CGRectMake(-50, 20, 100, 100);
    [appDelegate.MainToolBar addSubview:backToMenuBtn];

    backToMenuBtn.tag=66;
    [backToMenuBtn addTarget:self action:@selector(retrunBack) forControlEvents:UIControlEventTouchUpInside];
    [backToMenuBtn setImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateNormal];
    [backToMenuBtn setImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateHighlighted];
    [backToMenuBtn setBackgroundImage:[UIImage imageNamed:@"tap_light.png"] forState:UIControlStateHighlighted];
    backToMenuBtn.alpha=0.0f; 

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.2f];
    [UIView setAnimationDelay:0.4f];

    
    backToMenuBtn.alpha=1.0f; 

    
    
    [UIView commitAnimations];
    
    
    
}



//生成A到Z得横幅
- (void)setAToZChooseBar
{   
    
    UIImageView *background1;
    
    background1=[[UIImageView alloc]initWithImage:[UIImage  imageNamed:@"Main_background.png"]];
    background1.frame = CGRectMake(0, 0, 320, 480);
    [self.view addSubview:background1];
    backView = [[UIScrollView alloc] init ];
    [backView setFrame:CGRectMake(0, 460, 320, 480)];
    backView.contentInset = UIEdgeInsetsMake(0, 0, 200, 0);
    
    for (int i = 26; i >= 0; i--)
    {
        
        UIImageView *tempShadowView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Learn_shadow.png"]];
        [tempShadowView setFrame:CGRectMake(0, 15*i+i*44, 320, 110)];
        [tempShadowView setTag:i+200];
        
        UIImageView *tempView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Learn_common.png"]];
        [tempView setFrame:CGRectMake(0, 10+15*i+ i*44, 320, 44)];
        [tempView setTag:i+100];
        [tempView setUserInteractionEnabled:YES];
        
        UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [tempBtn setFrame:CGRectMake(0, 0, 320, 44)];
        
        
        [tempBtn setBackgroundColor:[UIColor clearColor]];
        
        tempBtn.titleLabel.font = [UIFont boldSystemFontOfSize:35];
        tempBtn.titleLabel.shadowOffset = CGSizeMake(3, 3);
        [tempBtn setTag:i+500];
        [tempBtn addTarget:self action:@selector(FilpToChoose:) forControlEvents:UIControlEventTouchUpInside];
        
        [backView addSubview:tempShadowView];
        [backView addSubview:tempView];
        
        [tempView addSubview:tempBtn];
        
        UILabel   *processLabelLeft=[[UILabel alloc]initWithFrame:CGRectMake(210, 8, 50, 30)];
        UILabel *midProcessLabel=[[UILabel alloc]initWithFrame:CGRectMake(110, 8,100, 30)];
        UILabel   *processLabelRight=[[UILabel alloc]initWithFrame:CGRectMake(260, 8, 50, 30)];
        midProcessLabel.tag=650+i;
        processLabelLeft.tag=450+i;
        processLabelRight.tag=550+i;
        midProcessLabel.backgroundColor=[UIColor clearColor];
        processLabelLeft.backgroundColor=[UIColor clearColor];
        processLabelRight.backgroundColor=[UIColor clearColor];
        
        [tempView addSubview:processLabelLeft];
        [tempView addSubview:processLabelRight];
        [tempView addSubview:midProcessLabel];
        
        
        [tempView release];
        [tempShadowView release];
        
        
    }
    
    
    for (int i=0; i<27; i++) {
        NSString *tempTitle=[[CacheData sharedData].letters objectAtIndex:i];
        NSString *tempWholeTitle=[[NSString alloc]initWithFormat:@"%@.png",tempTitle];
        
        UIImageView *tempPic=[[UIImageView alloc]initWithFrame:CGRectMake(140, 10+15*i+i*44, 44, 44)];
        if(i==26)
        {
            tempPic=[[UIImageView alloc]initWithFrame:CGRectMake(118, 10+15*i+i*44, 88, 49)];
            
            
        }
        [tempPic setImage:[UIImage imageNamed:tempWholeTitle]];
        tempPic.tag=250+i;  
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setFrame:CGRectMake(40, 10+15*i+i*44, 40, 40)];//(0, 15*i+i*44, 320, 110)   (40,0,40,40)
        [leftBtn setBackgroundColor:[UIColor clearColor]];
        [leftBtn setHidden:YES];
        [leftBtn setAlpha:0.0f];
        [leftBtn setTag:i+300];
        [leftBtn setBackgroundImage:[UIImage imageNamed:@"restart.png"] forState:UIControlStateNormal];
        [leftBtn setBackgroundImage:[UIImage imageNamed:@"tap_light.png"] forState:UIControlStateHighlighted];
        [leftBtn setImage:[UIImage imageNamed:@"restart.png"] forState:UIControlStateHighlighted];
        [leftBtn addTarget:self action:@selector(reBeginAnimation) forControlEvents:UIControlEventTouchUpInside];
        
        [backView addSubview:leftBtn];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setFrame:CGRectMake(250, 10+15*i+i*44, 40, 40)];
        [rightBtn setBackgroundColor:[UIColor clearColor]];
        [rightBtn setHidden:YES];
        [rightBtn setTag:i+400];
        [rightBtn setAlpha:0.0f];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"forward.png"] forState:UIControlStateNormal];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"tap_light.png"] forState:UIControlStateHighlighted];
        //[rightBtn setImage:[UIImage imageNamed:@"forward.png"] forState:UIControlStateHighlighted];
        [rightBtn setImage:[UIImage imageNamed:@"forward.png"] forState:UIControlStateHighlighted];
        [rightBtn addTarget:self action:@selector(nextAnimation) forControlEvents:UIControlEventTouchUpInside];
        
        [backView addSubview:rightBtn];        
        [backView addSubview:tempPic];
    }
    
    [backView setContentSize:CGSizeMake(320, 27*59+100)];
    [backView setUserInteractionEnabled:YES];
    
    [self.view addSubview:backView];
    [backView release];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.6f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationRepeatAutoreverses:NO];
    backView.frame=CGRectMake(0, 0, 320, 480);
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
    
}

-(void)retrunBack
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)reBeginAnimation
{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.8f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationRepeatAutoreverses:NO];
    backView.frame=CGRectMake(0, 460, 320, 460);
    [UIView setAnimationDidStopSelector:@selector(reBegin)];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
    
}
-(void)reBegin
{
    
    
    
    [[CacheData sharedData] loadSelectedLetter:flag-500 withFrom:HotWordViewFromBeginning];
    if ([CacheData sharedData].viewType == HotWordViewTypeLearn) {
        LearningViewController *learnWordViewController = [[LearningViewController alloc] init];
        
        [self.navigationController pushViewController:learnWordViewController animated:NO];
        
        [learnWordViewController release];
    }
    else
    {    
        PracticeViewController *practiceViewController = [[PracticeViewController alloc] init];
        
        for (int j = 0; j < 5; j++){
            [[practiceViewController.view viewWithTag:j+800 ]setFrame:CGRectMake(0, 460+20 + j*79, 320, 45)];
            [practiceViewController.view viewWithTag:j+700 ].frame=CGRectMake(0, 460+20 + j*79+49, 320, 80);
        }
        [self.navigationController pushViewController:practiceViewController animated:NO];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationRepeatAutoreverses:NO];
        for (int j = 0; j < 5; j++){
            [[practiceViewController.view viewWithTag:j+800 ]setFrame:CGRectMake(0, 20 + j*79, 320, 45)];
            [practiceViewController.view viewWithTag:j+700 ].frame=CGRectMake(0, 20 + j*79+49, 320, 80);
        }
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
        
        [practiceViewController release];
    }
}


-(void)nextAnimation
{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.8f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationRepeatAutoreverses:NO];
    backView.frame=CGRectMake(0, 480, 320, 480);
    [UIView setAnimationDidStopSelector:@selector(next)];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
    
    
    
}

-(void)next
{
    
    
    [[CacheData sharedData] loadSelectedLetter:flag-500 withFrom:HotWordViewFromLastTime];
    if ([CacheData sharedData].viewType == HotWordViewTypeLearn) {
        LearningViewController *learnWordViewController = [[LearningViewController alloc] init];  
        [self.navigationController pushViewController:learnWordViewController animated:NO];
        [learnWordViewController release];
    }
    else
    {
        PracticeViewController *practiceViewController = [[PracticeViewController alloc] init];
        
        
        for (int j = 0; j < 5; j++){
            [[practiceViewController.view viewWithTag:j+800 ]setFrame:CGRectMake(0, 460+30 + j*79, 320, 45)];
            [practiceViewController.view viewWithTag:j+700 ].frame=CGRectMake(0, 460+30 + j*79+49, 320, 80);
        }
        [self.navigationController pushViewController:practiceViewController animated:NO];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationRepeatAutoreverses:NO];
        for (int j = 0; j < 5; j++){
            [[practiceViewController.view viewWithTag:j+800 ]setFrame:CGRectMake(0, 30 + j*79, 320, 45)];
            [practiceViewController.view viewWithTag:j+700 ].frame=CGRectMake(0, 30 + j*79+49, 320, 80);
        }
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
        [practiceViewController release];
    }
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    flag=0;
    [self setAToZChooseBar];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
